<?php
/*--------------------------
	file:	form.class
	intro:	表单设计类
--------------------------*/
class Form extends Base{
/*-------------------------------------------
*	表单类 form.class
*	Author: corrie
*	生成表单布局
--------------------------------------------*/
	var $_formdata;
	var $_forminfo;
	var $_formarray;
	var	$_index = 1;
	var $_signer = 1;
	var $_token;

	function Form(){
		$this->_set();
	}
	
	function _set(){
		$_formname = 'field';
		$_formprename = 'prefield';
	}
	
	function debug(){
		$this->_debugshow($this->_sprint($this->_formarray));
	}
	
	function _reset(){
		$this->_formdata = "";
		$this->_forminfo = array();
		$this->_formarray = array();
		$this->_index = 1;
		$this->_signer = 1;
	}
	
	function signer($index){
		$this->_signer = $index;
	}
	
	//createelment(类型, name名称, 其他, 值, 附加数据, 标记)
	function createelment($type, $name, $add='', $value='', $list='', $signer=''){	
		if(!$signer || $signer=='')$signer=$this->_signer;
		if($name){
			if(strexists($name, '[]')){
				$name = str_replace('[]', '', $name);
				$strname = "field[{$signer}][{$name}][]";
				$prename = "prefield[{$signer}][{$name}][]";
				$attrid = "";
			}else{
				$strname = "field[{$signer}][{$name}]";
				$prename = "prefield[{$signer}][{$name}]";
				$attrid = "id='{$name}'";
				//$attrid = "";
			}
			$attrname = 'name="'.$strname.'"';
			$preattrname = 'name="'.$prename.'"';
		}else{
			$attrname = '';
		}
		$value && $preelement = "<input type='hidden' {$preattrname} value='{$value}' />";
		!$value && $preelement = '';
		switch($type){
			case "text":
				$element = "{$list}<input type='text' {$attrname} {$attrid} value='{$value}' {$add} />";
			break;
			case "password":
				$element = "{$list}<input type='password' {$attrname} {$attrid} value='{$value}' {$add} />";
			break;
			case "textarea":
				$element = "<textarea {$attrname} {$attrid} {$add}>{$value}</textarea>";
			break;
			case "file":
				$preelement = '';
				$element = "<input type='file' {$attrname} {$attrid} {$add} />";
			break;
			case "submit":
				$preelement = '';
				$element = "<input type='submit' {$attrname} {$attrid} value='{$value}' {$add} />";
			break;
			case "reset":
				$preelement = '';
				$element = "<input type='reset' {$attrname} {$attrid} value='{$value}' {$add} />";
			break;
			case "button":
				$preelement = '';
				$element = "<input type='button' {$attrname} {$attrid} value='{$value}' {$add} />";
			break;
			case "checkbox":
				$checked = '';
				if(!$list){
					$key = '1';
					$item = '';
				}else{
					list($key, $item) = explode(',', $list);
					if($key==$value){
						$checked = "checked='true'";
					}
				}
				$element = "<input type='checkbox' {$attrname} {$attrid} value='{$key}' {$checked} {$add} />{$item}";
			break;
			case "radio":
				$checked = '';
				if(!$list){
					$key = '1';
				}else{
					list($key, $item) = explode(',', $list);
					if($key==$value){
						$checked = "checked='true'";
					}
				}
				$element = "<input type='radio' {$attrname} {$attrid} value='{$key}' {$checked} {$add} />{$item}";
			break;
			case "select":
				if(!is_array($list)){
					if(!$list){
						$key = '0';
						$item = "No Items!";
					}else{
						list($key, $item) = explode(',', $list);
					}
					$element = "<select {$attrname} {$attrid} {$add}><option value='{$key}'>{$item}</option></select>";
				}else{
					$element = "<select {$attrname} {$attrid} {$add}>";
					foreach($list as $key=>$item){
						$element.= "<option value='{$key}'>{$item}</option>";
					}
					$element.= "</select>";
				}
			break;				
		}
		return $preelement.$element;
	}
	
	function createone($type, $name, $add='', $value='', $list='', $signer=''){
		if(is_array($list)){
			$one = '';
			switch($type){
				case "checkbox":
					foreach($list as $k=>$v){
						$one.= "<label class='{$type}'>".$this->createelment($type, $name, $add, $value, "{$k},{$v}", $signer)."</label>";
					}
				break;
				case "radio":
					foreach($list as $k=>$v){
						$one.= "<label class='{$type}'>".$this->createelment($type, $name, $add, $value, "{$k},{$v}", $signer)."</label>";
					}
				break;
				case "select":
					$one = "<label class='{$type}'>".$this->createelment($type, $name, $add, $value, $list, $signer)."</label>";
				break;
				case "text":
					foreach($list as $k=>$v){
						if(is_array($value)){
							$value = $value[$k];
						}
						$one.= "<label class='{$type}'>".$this->createelment($type, $name, $add, $value, "{$v}", $signer)."</label> ";
					}
				break;
				default: $one = "<label class='{$type}'>".$this->createelment($type, $name, $add, $value, $list, $signer)."</label>";
			}
		}else{
			$one = "<label class='{$type}'>".$this->createelment($type, $name, $add, $value, $list, $signer)."</label>";
		}
		return $one;
	}
	
	function createline($arr){
		$line = '';
		//print_r($arr);
		foreach($arr as $k=>$v){
			$line .= "$v";
		}
		return $line;
	}
	
	function addtoline($arr, $title='', $intro='', $add=''){
		$line = '';
		if(!$intro){
			$line.= "<div class='form_line {$add}'><div class='form_cols form_col_1'>{$title}</div><div for='none' class='form_cols form_col_2'>".$this->createline($arr)."</div><div class='clear'></div></div>\n";
		}else{
			$line.= "<div class='form_line {$add}'><div class='form_cols form_col_1'>{$title}</div><div for='none' class='form_cols form_col_2'>".$this->createline($arr)."</div><div class='form_cols form_col_3'>{$intro}</div><div class='clear'></div></div>\n";
		}
		return $line;
	}
	
	function addtoplat($arr, $title='', $add=''){
		$plat = "<div class='form_plat' {$add}>";
		//$plat.= join("\n", $arr);
		foreach($arr as $k=>$v){
			$plat.= "<span class='button'>{$v}</span>";
		}
		if($title){
			$plat.= "<div class='clear'></div><span class='form_plat_title'>{$title}</span>";
		}
		$plat.= "</div>";
		return $plat;
	}
	
	function addtogroup($arr, $title='', $add=''){
		if($title=='')
			$group = "<div class='form_group' {$add}>";
		else
			$group = "<div class='form_group' {$add}><div class='form_group_title'>{$title}</div>\n";
		$group.= join("\n", $arr);
		$group.= "</div>";
		return $group;
	}
	
	function addtoform($arr, $title='', $name='', $action='?', $method='post', $add='', $iftoken=''){
		if($iftoken=='1'){
			include _path('class/token');
			$token = Token::create();
			//echo $token;
		}
		$form = "<form action='{$action}' name='{$name}' id='id_{$name}' method='{$method}' $add>{$token}<div class='form_form'>\n";
		if($title!='')$form.= "<div class='form_title'>{$title}</div>\n";
		$form.= join("\n", $arr);
		//$form.= $arr;
		$form.= "</div></form>\n";
		return $form;
	}
	
	function addline($array, $title='', $intro=''){
		$len = count($this->_formarray);
		$this->_formarray[$len]['model'] = 'line';
		foreach($array as $key=>$value){
			$array[$key] = explode(',', stripblanks($value));
		}
		$this->_formarray[$len]['elements'] = $array;
		$this->_formarray[$len]['title'] = $title;
		$this->_formarray[$len]['intro'] = $intro;
		return $len;
	}
	function addgroup(){
		$arr = $args = func_get_args();
		unset($arr[0], $arr[1]);		
		$len = count($this->_formarray);			
		$this->_formarray[$len]['model'] = 'group';
		$this->_formarray[$len]['title'] = $args[0];
		$this->_formarray[$len]['add'] = $args[1];
		for($i=2; $i<=count($arr)+1; $i++){
			$array = array();
			foreach($arr[$i][0] as $key=>$value){
				$array[$key] = explode(',', stripblanks($value));
			}
			$this->_formarray[$len]['elements'][$i-2]['model'] = 'line';
			$this->_formarray[$len]['elements'][$i-2]['elements'] = $array;//$arr[$i][0];
			$this->_formarray[$len]['elements'][$i-2]['title'] = $arr[$i][1];
			$this->_formarray[$len]['elements'][$i-2]['intro'] = $arr[$i][2];
		}
		return $len;
	}
	function addplat($array, $intro='', $add=''){
		$len = count($this->_formarray);
		$this->_formarray[$len]['model'] = 'plat';
		foreach($array as $key=>$value){
			unset($array[$key]);
			$array[$key] = explode(',', stripblanks($value));
		}
		//print_r($narray);
		$this->_formarray[$len]['elements'] = $array;
		$this->_formarray[$len]['intro'] = $intro;
		$this->_formarray[$len]['add'] = $add;
		return $len;
	}
	function addform($title='', $name='', $action='?', $method='post', $add=''){
		//$this->_formdata = $this->addtoform($this->_formdata, $name, $title, $action, $add, $method);
		$this->_forminfo['name'] = $name;
		$this->_forminfo['title'] = $title;
		$this->_forminfo['action'] = $action;
		$this->_forminfo['add'] = $add;
		$this->_forminfo['method'] = $method;
	}
	
	//绑定列表到元素上
	function bindlist($name, $array){
		//$array_keys = array_keys($array);
		foreach($this->_formarray as $key=>$value){
			if($value['model'] == 'line' || $value['model'] == 'plat'){
				foreach($value['elements'] as $k=>$v){					
					if($v[1] == $name){
						$this->_formarray[$key]['elements'][$k][4] = $array;
					}
				}
			}elseif($value['model'] == 'group'){
				foreach($value['elements'] as $k=>$v){
					if($v['model'] == 'line'){
						foreach($v['elements'] as $i=>$val){					
							if($val[1] == $name){
								$this->_formarray[$key]['elements'][$k]['elements'][$i][4] = $array;
							}
						}
					}
				}
			}
		}
	}
	
	function arraytoline($array, $title='', $intro='', $index=''){
		$add = '';
		if($index%2==0)$add = 'odd';
		foreach($array as $k=>$v){
			$elems[] = $this->createone($v[0], $v[1], isset($v[2])?$v[2]:'', isset($v[3])?$v[3]:'', isset($v[4])?$v[4]:'', isset($v[5])?$v[5]:'');
		}
		$line = $this->addtoline($elems, $title, $intro, $add);
		//$this->_formdata.= $line;
		return $line;
	}
	
	function arraytoplat($array, $intro=''){
		foreach($array as $k=>$v){
			$elems[] = $this->createone($v[0], $v[1], isset($v[2])?$v[2]:'', isset($v[3])?$v[3]:'', isset($v[4])?$v[4]:'', isset($v[5])?$v[5]:'');
		}
		$line = $this->addtoplat($elems, $intro);
		return $line;
		//$this->_formdata.= $line;
	}
	
	function addtoken(){
		$this->_token = 1;
	}
	
	function buildform($array=array(), $isgroup=false){
		if(empty($array))$array = $this->_formarray;
		$lines = '';
		foreach($array as $k=>$val){
			if($val['model']=='line'){
				$line = $this->arraytoline($val['elements'], $val['title'], $val['intro'], $this->_index);
				$lines.= $line;
				$this->_index += 1;				
				if($isgroup==true){
					if(count($array)==$k+1){
						return $lines;
					}
				}else{
					$this->_formdata .= $line;
				}
				//$from.= $this->addtoline($array('type'));
			}elseif($val['model']=='plat'){
				$this->_formdata .= $this->arraytoplat($val['elements'], $val['intro'], $val['add']);
			}elseif($val['model']=='group'){
				$this->_formdata .= $this->addtogroup(array($this->buildform($val['elements'], true)), $val['title'], $val['add']);
				//echo $this->addtogroup(array($this->buildform($val)), $val['title']);;
				
			}
		}
		if(!empty($this->_forminfo)){
			$info = $this->_forminfo;
			$this->_formdata = $this->addtoform(array($this->_formdata), $info['title'], $info['name'], $info['action'], $info['method'], $info['add'], $this->_token ? '1':'');
		}
		return $this->_formdata;
	}
}
?>